<template>
	<el-main>
		<el-row :gutter="15">
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24" v-for="item in list" :key="item.storageId">
				<el-card class="storage storage-item" shadow="hover">
					<h2>{{ item.storageName }}</h2>
					<ul>
						<li>
							<p>管理员：{{ item.managerUserName }}<span style="float: right">电话:{{item.tel}}</span></p>
						</li>
						<li>
							<p class="info-title" :title="item.position">地址：{{ item.position }}</p>
						</li>
						<li>
							<h4>备注说明</h4>
							<div class="info-title" :title="item.remark">{{ item.remark }}</div>
						</li>
					</ul>
					<div class="bottom">
						<div class="state">
							<el-tag v-if="item.status=='1'" size="small">启用</el-tag>
							<el-tag v-if="item.status!='1'" size="small" type="info">停用</el-tag>
						</div>
						<div class="handler">
							<el-dropdown trigger="click">
								<el-button type="primary" icon="el-icon-more" circle plain></el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="edit(item)">编辑</el-dropdown-item>
										<el-dropdown-item @click="del(item)" divided>删除</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24">
				<el-card class="storage storage-add" shadow="never" @click="add">
					<el-icon>
						<el-icon-plus/>
					</el-icon>
					<p>创建仓库</p>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save'
export default {
	name: 'storage',
	components: {
		saveDialog
	},
	provide() {
		return {
			list: this.list
		}
	},
	data() {
		return {
			dialog: {
				save: false
			},
			list: []
		}
	},
	mounted() {
		this.getStorageList();
	},
	methods: {
		async getStorageList() {
			var res = await this.$API.office.fixed.getAllFixedAssetsStorageList.get();
			this.list = res.data;
		},
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		edit(storage) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(storage)
			})
		},
		async deleteStorage(storage) {
			console.log(storage)
			const loading = this.$loading();
			var res = await this.$API.office.fixed.deleteFixedAssetsStorage.post({storageId:storage.storageId});
			if (res.code == 200) {
				this.list.splice(this.list.findIndex(item => item.storageId === storage.storageId), 1)
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			loading.close();
		},
		async del(storage) {
			await this.$confirm(`确认删除 ${storage.storageName} 仓库吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.deleteStorage(storage);
			}).catch(() => {
				//取消
			})
		},
		async run(storage) {
			var params = {
				storageId:storage.storageId,
				status:"0"
			}
			var res = await this.$API.office.fixed.updateFixedAssetsStorage.post(params);
			if (res.code == 200) {
				storage.status="0";
				this.$message.success(`已成功执行计划任务：${storage.storageName}`)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.list.push(data)
			} else if (mode == 'edit') {
				this.list.filter(item => item.storageId === data.storageId).forEach(item => {
					Object.assign(item, data)
				})
			}
		}
	}
}
</script>

<style scoped>
.storage {
	height: 210px;
}

.storage-item h2 {
	font-size: 15px;
	color: #3c4a54;
	padding-bottom: 15px;
}

.storage-item li {
	list-style-type: none;
	margin-bottom: 10px;
}

.storage-item li h4 {
	font-size: 12px;
	font-weight: normal;
	color: #999;
}

.storage-item li p {
	margin-top: 5px;
}

.storage-item .bottom {
	border-top: 1px solid #EBEEF5;
	text-align: right;
	padding-top: 10px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.storage-add {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	text-align: center;
	cursor: pointer;
	color: #999;
}

.storage-add:hover {
	color: #409EFF;
}

.storage-add i {
	font-size: 30px;
}

.storage-add p {
	font-size: 12px;
	margin-top: 20px;
}

.dark .storage-item .bottom {
	border-color: var(--el-border-color-light);
}

.info-title {
	width: 100%;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	cursor: pointer;
}
</style>
